//
//  ViewController.m
//  demo38vedioToGif
//
//  Created by bing on 16/2/1.
//  Copyright © 2016年 bing. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVTime.h>
#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVAssetImageGenerator.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <QBImagePickerController/QBImagePickerController.h>
#import <UIView+Toast.h>

#define timeIntervalometer 0.2

@interface ViewController ()
<QBImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *webView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)start:(id)sender {
    QBImagePickerController *vc = [[QBImagePickerController alloc] init];
    vc.mediaType = QBImagePickerMediaTypeVideo;
    vc.maximumNumberOfSelection = 1;
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:nil];
}


// 获取视频路径
- (void)begin:(PHAsset *)asset {
    if (asset.mediaType == PHAssetMediaTypeVideo) {
        PHImageManager *manager = [PHImageManager defaultManager];
        [manager requestAVAssetForVideo:asset options:nil
                          resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info)
         {
             if ([asset isKindOfClass:[AVURLAsset class]])
             {
                 [self getPic:asset];
             }
         }];
    }
    
}

// 获取帧
- (void)getPic:(AVAsset *)asset {
    /*!
     CMTimeMake(a,b)                a当前第几帧,b每秒钟多少帧.当前播放时间a/b
     CMTimeMakeWithSeconds(a,b)     a当前时间,b每秒钟多少帧.
     */
    
    CMTime time = [(AVURLAsset *)asset duration];
    
    // 时间
    CGFloat second = time.value * 1.0 / time.timescale;
    NSMutableArray *array = [NSMutableArray array];
    
    // 取秒(每隔0.25取一张图片)
    for (CGFloat i = 0; i < second; i+=timeIntervalometer) {
        CMTime slice1 = CMTimeMakeWithSeconds(i, time.timescale);
        [array addObject:[NSValue valueWithCMTime:slice1]];
    }
    
    __block NSInteger index = 1;
    NSMutableArray *images = [NSMutableArray array];
    AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
    generator.appliesPreferredTrackTransform = YES;
    
    CMTime tol = CMTimeMakeWithSeconds([@(0.01) floatValue], [@(600) intValue]);
    generator.requestedTimeToleranceBefore = tol;
    generator.requestedTimeToleranceAfter = tol;
    [generator generateCGImagesAsynchronouslyForTimes:array
                                    completionHandler:^(CMTime requestedTime, CGImageRef  _Nullable imageref,
                                                        CMTime actualTime, AVAssetImageGeneratorResult result,
                                                        NSError * _Nullable error)
     {
         if (imageref) {
             @autoreleasepool {
                 UIImage *image = [UIImage imageWithCGImage:imageref];
                 NSData *data   = [self imageWithImage:image scaledToSize:CGSizeMake(216, 384)];
                 [data writeToFile:[self filePath:index] atomically:YES];
                 [images addObject:[UIImage imageWithData:data]];
             }
         }
         
         // 取完后处理照片
         if (index == array.count) {
             [self makeGIF:images];
         }
         index++;
     }];
}

// 文件路径
- (NSString *)filePath:(NSInteger)index {
    NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentStr = [document objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *textDirectory = [documentStr stringByAppendingPathComponent:@"gif"];
    [fileManager createDirectoryAtPath:textDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *path = [textDirectory stringByAppendingFormat:@"/test%03ld.gif", (long)index];
    return path;
}

// 压缩图片
- (NSData *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return UIImageJPEGRepresentation(newImage, 0.5);
}


// gif的制作
- (void)makeGIF:(NSMutableArray *)imgs {
    NSLog(@"thread:%@",[NSThread currentThread]);
    
    NSString *path = [self filePath:99999];
    NSLog(@"%@",path);
    
    //创建CFURL对象
    CFURLRef url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
    
    //通过一个url返回图像目标
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imgs.count, NULL);
    
    //设置gif的信息,播放间隔时间,基本数据,和delay时间
    NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:@{(NSString *)kCGImagePropertyGIFDelayTime:@(timeIntervalometer)}
                                                                forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    // 无限循环播放
    NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:@{(NSString *)kCGImagePropertyGIFLoopCount:@(0)}
                                                              forKey:(NSString *)kCGImagePropertyGIFDictionary];
    //合成gif
    for (UIImage *img in imgs) {
        @autoreleasepool {
            CGImageDestinationAddImage(destination, img.CGImage, (__bridge CFDictionaryRef)frameProperties);
        }
    }
    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)gifProperties);
    CGImageDestinationFinalize(destination);
    CFRelease(destination);
    [imgs removeAllObjects];
    imgs = nil;
    
    
//    UIImage *savedImage = [UIImage imageWithContentsOfFile:path];
//    UIImageWriteToSavedPhotosAlbum(savedImage, nil, nil, nil);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.view hideToastActivity];
        [self.view makeToast:@"视频转gif图片" duration:2.0 position:CSToastPositionCenter];
        NSURL *url = [NSURL URLWithString:path];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [self.webView loadRequest:request];
    });
}


#pragma mark - QBImagePickerControllerDelegate

- (void)qb_imagePickerController:(QBImagePickerController *)imagePickerController
          didFinishPickingAssets:(NSArray *)assets
{
    [self.view makeToastActivity:CSToastPositionCenter];
    PHAsset *asset = assets.firstObject;
    [self begin:asset];
    [imagePickerController dismissViewControllerAnimated:YES completion:nil];
}


- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController
{
    [imagePickerController dismissViewControllerAnimated:YES completion:nil];
}
@end
